<?php
define('KE', 1);
define('IN_SCRIPT', 1);
define('HESK_PATH', '../');
define('HESK_NO_ROBOTS', 1);

/* Get all the required files and functions */
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');
require(HESK_PATH . 'inc/database.inc.php');

hesk_session_start();
hesk_dbConnect();

if (!$_SESSION['isadmin']) {
    exit();
}

/* Print header */
require_once(HESK_PATH . 'inc/header.admin.inc.php');

/* Print admin navigation */
require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');

function generate_token() {
    mt_srand();
    $random = mt_rand();
    $token = md5($random);

    return $token;
}

$root_token = generate_token();

$edit = 0;
$json = '{
            "data" : "Pertanyaan pertama",
            "attr" : {
                "id" : "' . $root_token . '", "class" : "root"
            },
            "state" : "open",
            "children" : [
                { 
                    "data" : "Solusi/pertanyaan jika Yes",
                    "attr" : {
                        "id" : "' . generate_token() . '", "class" : "yes_node"
                    },
                    "state" : "open"
                },
                { 
                    "data" : "Solusi/pertanyaan jika No",
                    "attr" : {
                        "id" : "' . generate_token() . '", "class" : "no_node"
                    },
                    "state" : "open"
                }
            ]
        }';

$category = "Nama topik";
$description = "New description";

if (isset($_GET['id'])) {
    $edit = 1;
    $id = $_GET['id'];

    $result = hesk_dbQuery("SELECT * FROM hesk_diag_categories WHERE id = $id;");

    $row = hesk_dbFetchAssoc($result);

    $json = $row['tree'];
    $category = $row['name'];
    $parent_id = $row['parent_id'];
    $description = $row['description'];

    $result = hesk_dbQuery("SELECT * FROM hesk_diag_parent_cat WHERE id = $parent_id;");
    $row = hesk_dbFetchAssoc($result);
    $parent = $row['name'];

    //hesk_dbClose();
}
?>
<script type="text/javascript">
    function generate_token() {
        var random = Math.floor(Math.random()*100000);
        var token = hex_md5(random.toString());
                
        return token;
    }
            
    $(document).ready(function () {
        $("#save").click(function () {
            var json = $("#tree").jstree("get_json", -1);
            var jsonString = JSON.stringify(json);
            var category = $("#category").val();
            var description = $("#description").val();
            var parent = $("#parent").val();
            //document.write('Saving...');
<?php if ($edit == 0): ?>
                $.post("ke_submit.php", {"json": jsonString, "category": category, "description": description, "parent": parent}, function(data) {
<?php elseif ($edit == 1): ?>
                    $.post("ke_submit.php", {"json": jsonString, "category": category, "parent": parent, "description": description, "id": <?php echo $id; ?>}, function(data) {
<?php endif ?>
                    window.location = "te.php";
                });
            });
                
            $("#expand").click(function () {
                var node = $.jstree._focused().get_selected();
                var result = $.jstree._reference($("#tree"))._get_children(node);
                    
                if (result.length == 0) {
                    $("#tree").jstree("create", null, "inside", {"attr": {"id": generate_token(), "class": "no_node"}, "data": "No content"}, null, true);
                    $("#tree").jstree("create", null, "inside", {"attr": {"id": generate_token(), "class": "yes_node"}, "data": "Yes content"}, null, true);
                }
            });
                
            $("#remove").click(function () {
                $("#tree").jstree("remove"); 
            });
                
            $("#tree").bind("dblclick.jstree", function () {
                $("#tree").jstree("rename"); 
            });
                
            var root_id = "<?php echo generate_token(); ?>";
        
            var json = <?php echo $json; ?>;
                
            $("#tree").jstree({
                "json_data" : {
                    "data" : json
                },
                "plugins" : ["themes", "ui", "crrm", "json_data"],
                "core" : { "initially_open" : [ root_id ] }
            });
        });
</script>
<div id="container"><br />
    <div id="metadata">
        <table>
            <tr>
                <td>Kategori:</td>
                <td>
                    <select id="parent" name="parent">
                        <?php
                        if (isset($_GET['id'])) {
                            $sql = "SELECT * FROM `hesk_diag_parent_cat`";
                            $result = hesk_dbQuery($sql);

                            while ($row = hesk_dbFetchAssoc($result)) {
                                $name = $row['name'];

                                if ($name == $parent) {
                                    echo "<option selected>$name</option>";
                                } else {
                                    echo "<option>$name</option>";
                                }
                            }
                        } else {
                            $sql = "SELECT * FROM `hesk_diag_parent_cat`";
                            $result = hesk_dbQuery($sql);

                            while ($row = hesk_dbFetchAssoc($result)) {
                                $name = $row['name'];
                                echo "<option>$name</option>";
                            }
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Topik:</td>
                <td>
                            <input style="width: 300px;" type="text" id="category" name="category" value="<?php echo $category; ?>"></input>
                </td>
            </tr>
        </table>
        <input type="hidden" id="description" name="description" value="<?php echo $description; ?>"></input><br />
    </div>
    <div id="controls" style="padding: 5px;">
        <input type="button" style="float:left;" id="expand" value="Expand Node" class="button"></input>
        <input type="button" style="float:left;" id="remove" value="Remove Node" class="button"></input>
        <input type="button" style="float:left;" id="save" value="Save" class="button"></input>    
        <br style="clear: both" />
    </div><br />
    <div id="tree" style="width: 800px; overflow: auto; padding: 5px;">
    </div>
</div>

<?php
require_once(HESK_PATH . 'inc/footer.inc.php');
?>
